package com.medium.house.utils.excel;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

public class MethodTool {
    public static Object getValue(Object object, String methodName) throws Exception {
        Class c = object.getClass();
        Method m = c.getMethod(methodName, new Class[0]);
        return m.invoke(object, new Object[0]);
    }

    public static void setValue(Object object, Field field, String methodName, Object parameter) throws Exception {
        Class c = object.getClass();
        Method m = null;
        if ("java.lang.String".equals(field.getType().getName())) {
            String strs = parameter.toString();
            m = c.getDeclaredMethod(methodName, new Class[]{strs.getClass()});
            m.invoke(object, new Object[]{strs});
        }
        if ("java.lang.Integer".equals(field.getType().getName())) {
            Integer integer = Integer.valueOf(Integer.parseInt(parameter.toString()));
            m = c.getDeclaredMethod(methodName, new Class[]{integer.getClass()});
            m.invoke(object, new Object[]{integer});
        }

        if ("boolean".equals(field.getType().getName())) {
            Boolean booble = Boolean.valueOf(Boolean.parseBoolean(parameter.toString()));
            m = c.getDeclaredMethod(methodName, new Class[]{booble.getClass()});
            m.invoke(object, new Object[]{booble});
        }

        if ("java.sql.Date".equals(field.getType().getName())) {
            Date udate = new Date(Date.parse(parameter.toString()));
            java.sql.Date date = new java.sql.Date(udate.getTime());
            m = c.getDeclaredMethod(methodName, new Class[]{date.getClass()});
            m.invoke(object, new Object[]{date});
        }
        if ("java.util.Date".equals(field.getType().getName())) {
            Date date = new Date(Date.parse(parameter.toString()));
            m = c.getDeclaredMethod(methodName, new Class[]{date.getClass()});
            m.invoke(object, new Object[]{date});
        }
        if ("java.lang.Long".equals(field.getType().getName())) {
            Long long1 = Long.valueOf(Long.parseLong(parameter.toString()));
            m = c.getDeclaredMethod(methodName, new Class[]{long1.getClass()});
            m.invoke(object, new Object[]{long1});
        }
    }

    public static void setBooleanVal(Object object, String methodName, boolean parameter) throws Exception {
        Class c = object.getClass();
        Method m = c.getDeclaredMethod(methodName, new Class[]{Boolean.TYPE});

        m.invoke(object, new Object[]{Boolean.valueOf(parameter)});
    }

    public static String getMethodNameBySet(String property) {
        return "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
    }

    public static String getMethodNameByGet(String property) {
        return "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
    }
}
